package com.gzit;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class TestListProgress extends Activity {
	private Button testDo = null;
	private ListView progressList = null;
	private List<HashMap<String, Object>> list;
	private MyAdapter myAdapet;
	private MyHandler myHandler;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test_list_progress_activity);
		myHandler = new MyHandler();
		findView();
		addListerner();
		initList();
	}

	private void findView() {
		testDo = (Button) findViewById(R.id.btn_test);
		progressList = (ListView) findViewById(R.id.lst_progress);
	}

	private void addListerner() {
		testDo.setOnClickListener(new ClickEvent());
	}

	private void initList() {
		list = new ArrayList<HashMap<String, Object>>();
		HashMap<String, Object> dataA = new HashMap<String, Object>();
		dataA.put("title", "dataA");
		dataA.put("current", 0);
		list.add(dataA);

		HashMap<String, Object> dataB = new HashMap<String, Object>();
		dataB.put("title", "dataB");
		dataB.put("current", 0);
		list.add(dataB);
	

		
		
		myAdapet = new MyAdapter(TestListProgress.this, list);
		progressList.setAdapter(myAdapet);
	}
	

	// 模拟处理线程
	class UpdateRunnable implements Runnable {
		int id;
		int currentPos = 0;
		int delay;
		MyHandler handler;

		public UpdateRunnable(int id, int delay, MyHandler handler) {
			this.id = id;
			this.handler = handler;
			this.delay = delay;
		}

		@Override
		public void run() {
			// TODO Auto-generated method stub
			while (currentPos <= 100) {
				Message msg = handler.obtainMessage();
				msg.what = 1;
				msg.arg1 = id;
				msg.arg2 = currentPos;
				currentPos = currentPos + 5;
				msg.sendToTarget();
				try {
					Thread.sleep(delay);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	// 消息Handler用来更新UI
	class MyHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case 1:
				int id = msg.arg1;
				int current = msg.arg2;
				updateProgress(id, current);
				break;
			}
			super.handleMessage(msg);
		}

		private void updateProgress(int id, int currentPos) {
			HashMap<String, Object> dataTemp = list.get(id);
			dataTemp.put("current", currentPos);
			myAdapet.chargeProgress(id, dataTemp);
		}
	}

	// 按钮事件
	class ClickEvent implements View.OnClickListener {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case (R.id.btn_test):
				new Thread(new UpdateRunnable(0, 400, myHandler)).start();
				new Thread(new UpdateRunnable(1, 200, myHandler)).start();
				System.out.println("OnClick");
				break;
			}
		}
	}

	// List的显示
	class MyAdapter extends BaseAdapter {
		List<HashMap<String, Object>> list;
		LayoutInflater infl = null;

		public MyAdapter(Context context, List<HashMap<String, Object>> list) {
			this.infl = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			this.list = list;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			convertView = infl.inflate(R.layout.list_layout, null);
			ProgressBar progressBar = (ProgressBar) convertView
					.findViewById(R.id.progress);
			TextView titleView = (TextView) convertView
					.findViewById(R.id.title);
			
			HashMap<String, Object> detail = list.get(position);
			String t = (String) detail.get("title");
			titleView.setText(t);
			int progress = (Integer) detail.get("current");
			progressBar.setProgress(progress);
			return convertView;
		}

		// 改变进度，postion就是要改的那个进度
		public void chargeProgress(int postion, HashMap<String, Object> detail) {
			this.list.set(postion, detail);
			// System.out.println("charged:" + detail.get("current"));
			notifyDataSetChanged();
		}
	}
}